package com.atguigu.cloud.sevice.Impl;/**
 * @Auter zzh
 * @Date 2024/11/13
 */

import com.atguigu.cloud.entities.Admin;
import com.atguigu.cloud.entities.Merchant;
import com.atguigu.cloud.sevice.AdminService;
import com.atguigu.cloud.tool.load.page.PaginationResult;
import com.atguigu.cloud.tool.load.page.QueryObject.BaseQueryObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.atguigu.cloud.mapper.AdminMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Collections;
import java.util.List;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.sevice.Impl
 * @className: adminServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/13 17:32
 * @version: 1.0
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    AdminMapper adminMapper;

    @Override
    public int add(Admin admin) {
        adminMapper.insert(admin);
        return admin.getId();
    }

    @Override
    public int delete(Integer id) {
        return adminMapper.deleteById(id);
    }
    //根据id删除用户
//    public int delete(Integer id) {
//        // 创建QueryWrapper实例，并设置等于条件
//        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("id", id);
//
//        // 使用mapper执行查询，selectOne方法返回单个对象
//        int result_id= adminMapper.delete(queryWrapper);
//        return result_id;
//    }




    @Override
    public int update(Admin admin) {
        int id=admin.getId();
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id); // 假设根据id更新
        adminMapper.update(admin, updateWrapper);

        return  id;
    }

    //根据ID查询用户
    @Override
    public Admin getById(Integer id) {

        // 创建QueryWrapper实例，并设置等于条件
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        // 使用mapper执行查询，selectOne方法返回单个对象
        Admin admin = adminMapper.selectOne(queryWrapper);

        return admin;
    }

    //查询所有的管理员,不设任何条件
    @Override
    public List<Admin> getAll() {
        QueryWrapper<Admin> querywrapper = new QueryWrapper<>();
        List<Admin> list_admin = adminMapper.selectList(querywrapper);

        return list_admin;
    }

    @Override
    public boolean onlyName(String name) {
        QueryWrapper<Admin> querywrapper = new QueryWrapper<>();
        querywrapper.eq("name",name);
        return adminMapper.selectList(querywrapper).isEmpty();
    }

    @Override
    public Integer getIdByName(String name) {
        QueryWrapper<Admin> querywrapper = new QueryWrapper<>();
        querywrapper.eq("name",name).select("id");
        return  adminMapper.selectOne(querywrapper).getId();
    }


    @Override
    public PaginationResult<Admin> myPaginationQuery(Integer currentPage) {
        //为0排除
        if (currentPage==0){
            throw new RuntimeException("输入目标页号为0,这将不被允许");
        }
        //分页对象,包含分页大小和分页号数
        BaseQueryObject qo =new BaseQueryObject(currentPage);

        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();

        // 获取查询的记录数
        long totalCount = adminMapper.selectCount(queryWrapper);
        // 获得查询的目标结果集,(这里就用基础的mybatis实现了)
        List<Admin> list_admin = adminMapper.PaginationQuery(qo.getStart(),qo.getPageSize());

        // 如果总记录数为0，那么说明没数据就不用下一步查询了，提高效率。
        if (totalCount == 0){
            // 返回一个查询结果集，返回当前页、每页记录数、以及一个空的结果集
            return new PaginationResult<Admin>(qo.getCurrentPage(),qo.getPageSize(),totalCount, Collections.EMPTY_LIST);
        }

        // 返回一个查询结果集，返回当前页、每页记录数、以及结果集
        return new PaginationResult<Admin>(qo.getCurrentPage(),qo.getPageSize(),totalCount,list_admin);
    }
}
